/*
 * PanelEnemy.java
 *
 * Created on January 2, 2008, 8:08 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.UI;

import hextd.sprite.EnemySprite;
import hextd.util.Context;
import hextd.wave.Path;
import hextd.wave.PathEmpty;
import hextd.wave.Wave;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author  hylke
 */
public class PanelEnemy extends javax.swing.JPanel {

    private Vector<EnemySprite> enemies = new Vector<EnemySprite>();
    private Context contextLocal,  contextFull;
    private int pHeight = 0;
    private int pWidth = 0;
    private float scale = 0;
    private int gameTime = 0;

    /** Creates new form PanelEnemy */
    public PanelEnemy() {
        this.contextLocal = new Context(null);
    }

    public void setContext(Context c) {
        this.contextFull = c;

        initComponents();

        this.contextLocal.contextUrl = c.contextUrl;
        this.contextLocal.colors = c.colors;
    }

    public void clearEnemies() {
        this.enemies.clear();
        this.contextLocal.getPaths().clear();
    }

    public void addEnemy(Wave.EnemyInstance e) {
        Vector<Path> paths = this.contextLocal.getPaths();
        PathEmpty p = new PathEmpty();
        paths.add(p);
        this.enemies.add(e.type.getCopy(this.contextLocal, paths.size() - 1, 0, e.health, e.price, e.size));
    }

    public Dimension getPreferredSize() {
        return new Dimension(195, 40);
    }

    public void recalculateSize() {
        this.pWidth = this.getWidth();
        this.contextLocal.getCache().clearCache();
        if (this.enemies.size() > 0) {
            this.pHeight = Math.min(this.pWidth / this.enemies.size(), this.getHeight());
            this.scale = this.pHeight;
            this.contextLocal.paintScale = this.scale;
            this.contextLocal.strokeScale = this.scale / 40f;
            int nr = 0;
            float x = 0;
            float y = 0.5f;
            for (Iterator<Path> i = this.contextLocal.getPaths().iterator(); i.hasNext();) {
                PathEmpty p = (PathEmpty) i.next();
                x = 0.5f + nr;
                p.setPoint(x, y);
                nr++;
            }
            for (Iterator<EnemySprite> i = this.enemies.iterator(); i.hasNext();) {
                EnemySprite e = i.next();
                e.resetScale();
            }
        }
    }

    public void doTick(int gameTime) {
        this.gameTime = gameTime;
        for (Iterator<EnemySprite> i = this.enemies.iterator(); i.hasNext();) {
            EnemySprite e = i.next();
            e.doTick(gameTime);
        }
        this.repaint();
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        if (this.enemies.size() > 0) {
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.BLACK);
            g2.fillRect(0, 0, this.pWidth, this.pHeight);
            synchronized (this.enemies) {
                for (Iterator<EnemySprite> i = this.enemies.iterator(); i.hasNext();) {
                    EnemySprite e = i.next();
                    e.paint(g2, this.gameTime);
                }
            }
        } else {
            g2.setColor(Color.BLACK);
            g2.fillRect(0, 0, this.getWidth(), this.getHeight());
        }
    }

    private void mouseOver(int x) {
        int nr = (int) (x / this.scale);
        if (nr < this.enemies.size()) {
            EnemySprite e = this.enemies.get(nr);
            this.contextFull.setInfoText(e.getInfoString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(this.contextFull.colors.getColorBackGround());
        setBorder(null);
        setForeground(this.contextFull.colors.getColorForeGround());
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
        this.mouseOver(evt.getX());
    }//GEN-LAST:event_formMouseMoved

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        this.recalculateSize();
    }//GEN-LAST:event_formComponentResized
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
